package com.litbw.user;

/**
 * DataValidator.java
 */

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * This is intended to be a helper class to help with data validation for the
 * user class/GUI when accepting user input.
 * 
 * @author Derrick Mink
 */
public final class DataValidator {
	private static final Pattern PASSWORD = Pattern
			.compile("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{8,}$");
	private static final Pattern EMAIL_PATTERN = Pattern
			.compile("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
					+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");

	private static final Pattern PHONE_PATTERN = Pattern
			.compile("\\d{3}-\\d{7}");
	private static final Pattern FIRST_NAME = Pattern.compile("[A-Z][a-zA-Z]*");
	private static final Pattern LAST_NAME = Pattern
			.compile("[a-zA-z]+([ '-][a-zA-Z]+)*");
	private static final Pattern ADDRESS = Pattern.compile("[a-zA-Z0-9\\s.]+");
	private static final Pattern CITY = Pattern
			.compile("([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)");
	private static final Pattern STATE = Pattern
			.compile("([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)");
	private static final Pattern ZIP = Pattern.compile("^\\d{5}(-\\d{4})?$");

	private static Matcher matcher;

	/**
	 * The password policy is: At least 8 characters Contains at least one digit
	 * Contains at least one lower alpha char and one upper alpha char Contains
	 * at least one char within a set of special char (@#%$^ etc.) Not
	 * containing blank, tab etc.
	 * 
	 * @param password
	 *            The password
	 * @return true for accepted
	 */
	public static boolean isValidPassword(final String password) {
		if (password != null) {
			matcher = PASSWORD.matcher(password);
			return matcher.matches();
		}
		return false;
	}

	public static boolean isValidEmail(final String address) {
		if (address != null) {
			matcher = EMAIL_PATTERN.matcher(address);
			return matcher.matches();
		}
		return false;
	}

	public static boolean isValidPhone(String phone) {
		if (phone != null) {
			phone = formatPhone(phone);
			matcher = PHONE_PATTERN.matcher(phone);
			return matcher.matches();
		}
		return false;
	}

	public static boolean isValidLast(String last) {
		if (last != null) {
			last = formatName(last);
			matcher = LAST_NAME.matcher(last);
			return matcher.matches();
		}
		return false;
	}

	public static boolean isValidFirst(String first) {
		if (first != null) {
			first = formatName(first);
			matcher = FIRST_NAME.matcher(first);
			return matcher.matches();
		}
		return false;
	}

	public static boolean isValidAddress(final String address) {
		if (address != null) {
			matcher = ADDRESS.matcher(address);
			return matcher.matches();
		}
		return false;
	}

	public static boolean isValidCity(final String city) {
		if (city != null) {
			matcher = CITY.matcher(city);
			return matcher.matches();
		}
		return false;
	}

	public static boolean isValidState(final String state) {
		if (state != null) {
			matcher = STATE.matcher(state);
			return matcher.matches();
		}
		return false;
	}

	public static boolean isValidZip(final String zip) {
		if (zip != null) {
			matcher = ZIP.matcher(zip);
			return matcher.matches();
		}
		return false;
	}

	public static boolean isValidName(String first, String last) {
		if (first != null && last != null) {
			return isValidFirst(first) && isValidLast(last);
		}
		return false;
	}

	public static String formatPhone(String phone) {
		if(phone == null)
			return null;
		String s = "";
		for (int i = 0; i < phone.length(); i++) {
			char c = phone.charAt(i);
			if (c > 47 && c < 58) {
				s += c;
			}
		}
		s = s.substring(0, 3) + '-' + s.substring(3);
		return s;
	}

	public static String formatName(String name) {
		if (name == null)
			return null;
		String s = "";
		s += Character.toUpperCase(name.charAt(0));
		s += name.toLowerCase().substring(1);
		return s;
	}
	public static void main(String[] args) {
		// System.out.println(isValidAddress("3008 N 8th st"));
		// System.out.println(isValidAddress("123 Main St"));
		// System.out.println(isValidAddress("123 Main St"));
		// System.out.println(isValidAddress("123 Main Street"));
		// System.out.println(isValidAddress("123 NW Main"));
		// System.out.println(isValidAddress(" 123 Main St North"));
		// System.out.println(isValidAddress("123 Main St. Apt. 4"));
		// System.out.println(isValidAddress("123 Main St. Suite 456"));
		String phone = "(253) - 5762159";
		isValidPhone(phone);
		System.out.println(formatPhone(phone));
		System.out.println(isValidFirst(null));
		System.out.println(isValidLast("Morris"));

	}
}